import { getTreeByList } from '@/utils'
import { request } from './request'

interface TreeItemVo {
  id: string
  pid: string
  name: string
}

export const getTreeData = async (params: TPagination<object>) => {
  const { data: list } = await request<TreeItemVo[]>({
    url: '/code/associationBoss/manager/entOrganizationDict/zxqTreeData',
    method: 'get',
    params
  })
  if (list) {
    return getTreeByList<TreeItemVo>({
      list,
      parentId: '0',
      parentKey: 'pid',
      handleItem(item) {
        item.selectable = !['0', '1', '2'].includes(item.pid) // pid为0，1的是平台类型，不属于协会
        return item
      }
    })
  }
}

export type TEntListParams = TPagination<{
  associationId: string
  entName: string
}>

export interface EntInfoRecord {
  entId: string
  entName: string
  staffCount: number
  driverCount: number
  carCount: number
  registerPersonCount: number
  registerCarCount: number
  relationId: string
}

export const getEntList = async (data: TEntListParams) => {
  const { data: res } = await request<TableData<EntInfoRecord>>({
    url: '/code/associationBoss/manager/register/entList',
    method: 'post',
    data
  })
  return res
}

export type TPersonListParams = TPagination<{
  associationId: string
  name?: string
  phone?: string
  coopType?: string // 41- "自有司机",42- "外协司机"
  entId?: string // 企业id 查询企业已备案人员时传
}>

export interface PersonInfoRecord {
  personId: string
  phone: string
  dateTime: string
  relationId: string
  coopType: string
  registerPersonCount: string
  joinList: Array<{
    name: string
    entId: string
  }>
}

export const getPersonList = async (data: TPersonListParams) => {
  const { data: res } = await request<TableData<PersonInfoRecord>>({
    url: '/code/associationBoss/manager/register/personList',
    method: 'post',
    data
  })
  return res
}

export type TCarListParams = TPagination<{
  associationId: string
  carBrand?: string
  carType?: string
  plateColor?: string
  coopType?: string // 51- "企业车辆",52- "外协车辆"
  entId?: string // 企业id 查询企业已备案车辆时传
}>

export interface CarInfoRecord {
  carId: string
  carBrand: string
  carType: string
  plateColor: string
  dateTime: string
  belongObjectNames: string
  relationId: string
  coopType: string
}

export const getCarList = async (data: TCarListParams) => {
  const { data: res } = await request<TableData<CarInfoRecord>>({
    url: '/code/associationBoss/manager/register/carList',
    method: 'post',
    data
  })
  return res
}

// 删除备案企业
export const deleteEnt = (data: { associationId: string; entId: string }) =>
  request({
    url: '/code/associationBoss/manager/removeEnt',
    method: 'post',
    data
  })

// 新增企业备案到协会
export const registerEnt = (data: { associationId: string; entIds: string }) =>
  request({
    url: '/code/associationBoss/manager/registerEnt',
    method: 'post',
    data
  })

// 查询企业列表
export const searchEntList = async (
  data: TPagination<{ entName?: string; associationId?: string }>
) => {
  const { data: res } = await request<
    TableData<{ entId: string; name: string }>
  >({
    url: '/code/associationBoss/manager/searchEntList',
    method: 'post',
    data: {
      filterFlag: 1,
      ...data
    }
  })
  return res
}
